package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.modular.crm.entity.GwdcT;
import cn.e4j.modular.crm.entity.GwdczT;
import cn.e4j.modular.crm.service.impl.Gwdc003ServiceImpl;
import cn.e4j.modular.utils.CrmExceptionEnum;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 非组合促销活动维护
 * Created by miaoyl on 2019-1-28.
 */
@Controller
@RequestMapping("/crm/gwdc003")
public class Gwdc003Controller extends BaseController {

    @Autowired
    private Gwdc003ServiceImpl gwdc003Service;

    /**
     * 跳转非组合促销活动管理页面
     */
    @RequiresPermissions("crm:gwdc003:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwdc003.html";
    }

    /**
     * 查询非组合促销活动信息列表
     */
    @RequestMapping("/getGwdclist")
    @RequiresPermissions("crm:gwdc003:init")
    @ResponseBody
    public ResponseData getGwdclist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        if(inDto.getString("GWDC016_1") != null && inDto.getString("GWDC016_1") != ""){
            inDto.put("type",'1');
        }
        if(inDto.getString("GWDC016_2") != null && inDto.getString("GWDC016_2") != ""){
            inDto.put("type",'2');
        }
        if(inDto.getString("GWDC016_1") != null && inDto.getString("GWDC016_1") != "" &&
                inDto.getString("GWDC016_2") != null && inDto.getString("GWDC016_2") != ""){
            inDto.put("type",'3');
        }
        List<Map<String, Object>> result = gwdc003Service.getGwdclist(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询非组合促销活动商品信息列表
     */
    @RequestMapping("/getGwdcData")
    @ResponseBody
    public ResponseData getGwdcData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdc003Service.getGwdcData(inDto);
        return ResponseData.success(result);
    }

    /**
     * 查询非组合促销活动赠品信息列表
     */
    @RequestMapping("/getGwdczData")
    @ResponseBody
    public ResponseData getGwdczData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdc003Service.getGwdczData(inDto);
        return ResponseData.success(result);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @RequiresPermissions("crm:gwdc003:init")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwdcT> gwdcTList = JSONObject.parseArray(jsonObject.getString("list1"), GwdcT.class);
        List<GwdczT> gwdczTList = JSONObject.parseArray(jsonObject.getString("list2"), GwdczT.class);
        Dto outDto = gwdc003Service.addData(gwdcTList,gwdczTList,jsonObject.getString("GWDC001"));
        if(outDto.getAppCode() == 401){
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWDC.getCode() ,
                            CrmExceptionEnum.EXISTED_THE_GWDC.getMessage());
        }else if(outDto.getAppCode() == 402){
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWDCZ.getCode() ,
                            CrmExceptionEnum.EXISTED_THE_GWDCZ.getMessage());
        }
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    @RequiresPermissions("crm:gwdc003:init")
    @ResponseBody
    public ResponseData edit(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwdcT> gwdcTList = JSONObject.parseArray(jsonObject.getString("list1"), GwdcT.class);
        List<GwdczT> gwdczTList = JSONObject.parseArray(jsonObject.getString("list2"), GwdczT.class);
        Dto outDto = gwdc003Service.editData(gwdcTList,gwdczTList,jsonObject.getString("GWDC001"));
        if(outDto.getAppCode() == 401){
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWDC.getCode() ,
                    CrmExceptionEnum.EXISTED_THE_GWDC.getMessage());
        }else if(outDto.getAppCode() == 402){
            return new ErrorResponseData(CrmExceptionEnum.EXISTED_THE_GWDCZ.getCode() ,
                    CrmExceptionEnum.EXISTED_THE_GWDCZ.getMessage());
        }
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwdc003:init")
    @ResponseBody
    public ResponseData del(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        gwdc003Service.delete(inDto);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwdc003:init")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwdcT> gwdcTList = inDto.getReqList(GwdcT.class);
        gwdc003Service.delDataBatch(gwdcTList);
        return SUCCESS_TIP;
    }

    /**
     * 修改单据状态
     */
    @RequestMapping("/updateData")
    @RequiresPermissions("crm:gwdc003:init")
    @ResponseBody
    public ResponseData updateData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwdcT> gwdcTList = inDto.getReqList(GwdcT.class);
        gwdc003Service.updateData(gwdcTList);
        return SUCCESS_TIP;
    }

    /**
     * 获取新增数据
     */
    @RequestMapping("/getAddData")
    @RequiresPermissions("crm:gwdc003:init")
    @ResponseBody
    public ResponseData getAddData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto addDataDto = gwdc003Service.getAddData(inDto);
        return new SuccessResponseData(addDataDto);
    }

    /**
     * 查询结算价
     */
    @RequestMapping("/getGwdc023")
    @ResponseBody
    public ResponseData getGwdc023(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwdcT> select_data = inDto.getReqList(GwdcT.class);
        List<Dto> list = new ArrayList<>();
        for(GwdcT gwdcT:select_data){
            inDto.put("GWDC007",gwdcT.getGWDC007());
            inDto.put("GWDC016",gwdcT.getGWDC016());
            inDto.put("GWDC018",gwdcT.getGWDC018());
            inDto.put("GWDC008",gwdcT.getGWDC008());
            Dto dto = gwdc003Service.getGwdc023(inDto);
            if(dto != null){
                list.add(dto);
            }
        }
        return ResponseData.success(list);
    }

    /**
     * 查询赠品结算价
     */
    @RequestMapping("/getZpGwdc023")
    @ResponseBody
    public ResponseData getZpGwdc023(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwdcT> select_data = inDto.getReqList(GwdcT.class);
        List<Dto> list = new ArrayList<>();
        for(GwdcT gwdcT:select_data){
            inDto.put("GWDC007",gwdcT.getGWDC007());
            inDto.put("GWDC016",gwdcT.getGWDC016());
            inDto.put("GWDC018",gwdcT.getGWDC018());
            inDto.put("GWDC008",gwdcT.getGWDC008());
            Dto dto = gwdc003Service.getZpGwdc023(inDto);
            list.add(dto);
        }
        return ResponseData.success(list);
    }

    /**
     * 修改失效日期
     */
    @RequestMapping("/editGwdc010")
    @RequiresPermissions("crm:gwdc003:init")
    @ResponseBody
    public ResponseData editGwdc010(GwdcT gwdcT) {
        gwdc003Service.editGwdc010(gwdcT);
        return SUCCESS_TIP;
    }

    @RequestMapping("/getGwdc001Data")
    @ResponseBody
    public ResponseData getGwdc001Data(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdc003Service.getGwdc001Data(page, inDto);
        return new PageReData(page, result);
    }
}
